#!/bin/bash
#
# 4shared.com module
# Copyright (c) 2010-2012 Plowshare team
#
# This file is part of Plowshare.
#
# Plowshare is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Plowshare is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Plowshare.  If not, see <http://www.gnu.org/licenses/>.

MODULE_DACLIPS_IN_REGEXP_URL="http://\(www\.\)\?daclips\.in/"

MODULE_DACLIPS_IN_DOWNLOAD_OPTIONS=""
MODULE_DACLIPS_IN_DOWNLOAD_RESUME=no
MODULE_DACLIPS_IN_DOWNLOAD_FINAL_LINK_NEEDS_COOKIE=no

MODULE_DACLIPS_IN_LIST_OPTIONS=""

daclips_in_download() {

local COOKIEFILE=$1
local URL=$2

if match '404.html' "$(curl -i "$URL")" || match '404 - Not Found' "$(curl -L "$URL")" ; then
        return $ERR_LINK_DEAD
fi

	Pageinit=$(curl -L "$URL") || return

id=$(echo "$Pageinit" | parse_form_input_by_name 'id') || return
#fname=$(echo "$Pageinit" | parse_form_input_by_name 'fname') || return
fname=$(echo "$Pageinit" | grep fname | sed 's/.*value="//g' | sed 's/">//g')
fname=$(echo $fname | sed 's/ /+/g')
# echo $fname > fname
Pageauth=$(curl -e "$URL" -d "op=download1&usr_login=&id=$id&fname=$fname&referer=&method_free=Free+Download" "$URL" ) || return
fname=$(echo $fname | sed 's/+/ /g')

# echo $Pageauth > pa

finallink=$(echo "$Pageauth" | break_html_lines_alt | grep 'file: ' )
# echo $finallink > fl0
finallink=$(echo "$finallink" | sed "s/^\/\/.*file: '//g" | grep "file: '" ) #| sed "s/', plugins.*//g"
# echo $finallink > fl1
finallink=$( echo "$finallink" | strip )
finallink=$( echo "$finallink" | replace "file: '" "")
finallink=$( echo "$finallink" | replace "'," "")

#finallink=${finallink:1}
# num=${#finallink}-650
# finallink=${finallink:0:$num}

# echo "$finallink" > finallink

	FILE_URL=$finallink
	FILENAME=$fname
	
	if match '404.html' "$(curl -r 0-5 "$FILE_URL")" || match '404 Not Found' "$(curl -r 0-5 "$FILE_URL")" ; then
        return $ERR_LINK_DEAD
  fi

wait 1 seconds	
	
	echo $FILE_URL 
	echo "$FILENAME"
	
	}
